<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>采购单管理</title>
    <link href="../layui/css/layui.css" rel="stylesheet">
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <!-- 头部区域 -->
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">小药店管理系统</div>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a href="javascript:;">
                        <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                        欢迎, <span id="username">管理员</span>
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" onclick="logout()">退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>

        <!-- 左侧导航区域 -->
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a href="javascript:;">采购管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../supplier/supplier.html">供应商信息管理</a></dd>
                            <dd><a href="../supplier/drug.html">药品信息管理</a></dd>
                            <dd><a href="add.html">采购单添加</a></dd>
                            <dd><a href="list.html">采购单管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">销售管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../sales/add_xiaoshoudan.html">销售单添加管理</a></dd>
                            <dd><a href="../sales/xiaoshoumingxi.html">销售明细管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">药店统计管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../statistics/instock.html">入库统计</a></dd>
                            <dd><a href="../statistics/sales.html">销售统计</a></dd>
                            <dd><a href="../statistics/stock.html">库存查询</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">人事管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">员工添加</a></dd>
                            <dd><a href="javascript:;">员工管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">打印管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../print/ruku.html">入库单</a></dd>
                            <dd><a href="../print/chuku.html">出库单</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">系统管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../user/xiuGaiMiMa.html">修改密码</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
        
        <!-- 内容主体区域 -->
        <div class="layui-body">
            <div style="padding: 15px;">
                <!-- 面包屑导航 -->
                <div class="layui-card">
                    <div class="layui-card-body">
                        <span class="layui-breadcrumb">
                            <a href="../index.html">首页</a>
                            <a><cite>采购单管理</cite></a>
                        </span>
                    </div>
                </div>

                <!-- 搜索栏 -->
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">采购单号</label>
                            <div class="layui-input-block">
                                <input type="text" name="danjuhao" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">负责人</label>
                            <div class="layui-input-block">
                                <input type="text" name="jingshouren" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <!-- 数据表格 -->
                <table id="dataTable" lay-filter="dataTable"></table>
            </div>
        </div>
    </div>
    
    <!-- 工具栏模板 -->
    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加采购单</button>
        </div>
    </script>
    
    <!-- 行工具栏模板 -->
    <script type="text/html" id="barTpl">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    
    <script src="../layui/layui.js"></script>
    <script>
    layui.use(['table', 'form', 'layer', 'element'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        var element = layui.element;
        
        // 初始化表格
        table.render({
            elem: '#dataTable',
            url: '../api/procurement/list',
            toolbar: '#toolbar',
            request: {
                pageName: 'page',
                limitName: 'limit'
            },
            parseData: function(res){
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count,
                    "data": res.data.data
                };
            },
            cols: [[
                {field: 'danjuhao', title: '采购单号', width: 180},
                {field: 'shijian', title: '采购时间', width: 160, templet: function(d){
                    return d.shijian ? layui.util.toDateString(new Date(d.shijian), 'yyyy-MM-dd') : '';
                }},
                {field: 'zongjiage', title: '总金额', width: 120},
                {field: 'jingshouren', title: '负责人', width: 100},
                {field: 'beizhu', title: '备注'},
                {fixed: 'right', title: '操作', toolbar: '#barTpl', width: 150}
            ]],
            page: true
        });
        
        // 工具栏事件
        table.on('toolbar(dataTable)', function(obj){
            if(obj.event === 'add'){
                window.location.href = 'add.html';
            }
        });
        
        // 行工具栏事件
        table.on('tool(dataTable)', function(obj){
            if(obj.event === 'del'){
                layer.confirm('确定删除此采购单？', function(index){
                    deleteProcurement(obj.data.id);
                    layer.close(index);
                });
            }
        });
        
        // 搜索
        form.on('submit(search)', function(data){
            table.reload('dataTable', {
                where: data.field
            });
            return false;
        });
        
        // 删除采购单
        function deleteProcurement(id){
            $.ajax({
                url: '../api/procurement/delete/' + id,
                type: 'DELETE',
                success: function(res){
                    if(res.code === 0){
                        table.reload('dataTable');
                    } else {
                        layer.msg(res.msg || '删除失败', {icon: 2});
                    }
                }
            });
        }
    });

    // 添加退出功能
    function logout() {
        layui.use(['jquery', 'layer'], function(){
            var $ = layui.$;
            var layer = layui.layer;
            
            $.ajax({
                url: '/user/logout',
                type: 'GET',
                success: function(res) {
                    if(res.code === 0) {
                        window.location.href = '/login/login.html';
                    } else {
                        layer.msg(res.msg || '退出失败', {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('服务器错误', {icon: 2});
                }
            });
        });
    }
    </script>
</body>
</html> 